package com.cuiweiyou.bluemesh.ui;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cuiweiyou.bluemesh.R;
import com.cuiweiyou.bluemesh.model.LightModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by macpro on 2018/10/24.
 */

public class DevicesInitAdapter extends RecyclerView.Adapter {

    private OnDeviceLongClickListener onDeviceLongClickListener;
    private List<LightModel> lightModelList;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(parent.getContext(), R.layout.item_init_tooth_content, null);
        return new BluetoothHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        LightModel light = lightModelList.get(position);
        String mile = light.getMile();
        String nickName = light.getNickname();

        BluetoothHolder holder = (BluetoothHolder) viewHolder;
        holder.macTextView.setText(nickName);
        holder.rssiTextView.setText(mile);
    }

    @Override
    public int getItemCount() {
        return null == lightModelList ? 0 : lightModelList.size();
    }

    public void updateDevices(List<LightModel> list) {
        if (null != lightModelList) {
            this.lightModelList.clear();
        }
        this.lightModelList = new ArrayList<>(list);

        notifyDataSetChanged();
    }

    class BluetoothHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener {

        private View deviceLayout;
        private TextView macTextView;
        private TextView rssiTextView;

        public BluetoothHolder(View view) {
            super(view);

            deviceLayout = view.findViewById(R.id.deviceLayout);
            macTextView = view.findViewById(R.id.macTextView);
            rssiTextView = view.findViewById(R.id.rssiTextView);

            deviceLayout.setOnLongClickListener(this);
        }

        @Override
        public boolean onLongClick(View v) {
            int position = getLayoutPosition();
            LightModel model = lightModelList.get(position);

            if (null != onDeviceLongClickListener) {
                onDeviceLongClickListener.onDeviceLongClick(model);
                return true;
            }

            return false;
        }
    }

    public void setOnDeviceLongClickListener(OnDeviceLongClickListener listener) {
        this.onDeviceLongClickListener = listener;
    }

    public interface OnDeviceLongClickListener {
        void onDeviceLongClick(LightModel model);
    }
}
